package com.linkoog.devtools.dubbo.toolwindow.workbenche.left;

import com.intellij.openapi.Disposable;
import com.intellij.openapi.project.Project;
import com.intellij.ui.CollectionListModel;
import com.intellij.util.ui.JBUI;
import com.linkoog.devtools.dubbo.invoke.DubboRequest;
import com.linkoog.devtools.dubbo.persistent.DubboFavouritePersistent;
import com.linkoog.devtools.dubbo.toolwindow.workbenche.left.list.DubboFavoritesJList;
import com.linkoog.devtools.dubbo.toolwindow.workbenche.left.list.DubboHistoryJList;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import java.util.function.Consumer;

public class DubboTestLeftPanel implements Disposable {
    private Project project;

    private JPanel myMainPanel;
    private JTabbedPane tabbedPane1;

    private CollectionListModel<DubboFavorite> favoritesListModel;
    private JList<DubboFavorite> favoritesList;
    private Consumer<DubboFavorite> favoritesClickCallback;

    private CollectionListModel<DubboHistory> historyListModel;
    private JList<DubboHistory> historyList;
    private Consumer<DubboHistory> historyClickCallback;

    public DubboTestLeftPanel(Project project) {
        this.project = project;
        myMainPanel.setMinimumSize(JBUI.size(215, -1));
        myMainPanel.setPreferredSize(JBUI.size(215, -1));
    }

    public void addFavorites(DubboFavorite favorite){
        DubboFavouritePersistent dubboFavouritePersistent = DubboFavouritePersistent.getInstance(project);
        dubboFavouritePersistent.getDubboFavorites().add(favorite);
        favoritesListModel.add(favorite);
        favoritesList.updateUI();
    }

    public void addHistory(DubboHistory history){
        historyListModel.add(history);
        historyList.updateUI();
    }

    public void setFavoritesClickCallback(Consumer<DubboFavorite> favoritesClickCallback) {
        this.favoritesClickCallback = favoritesClickCallback;
    }

    public void setHistoryClickCallback(Consumer<DubboHistory> historyClickCallback) {
        this.historyClickCallback = historyClickCallback;
    }


    public JPanel getMyMainPanel() {
        return myMainPanel;
    }

    private void createUIComponents() {
        DubboFavouritePersistent dubboFavouritePersistent = DubboFavouritePersistent.getInstance(project);
        favoritesListModel = new CollectionListModel<>(dubboFavouritePersistent.getDubboFavorites());
        favoritesList = new DubboFavoritesJList(favoritesListModel);
        favoritesList.addListSelectionListener(e -> {
            int row = favoritesList.getSelectedIndex();
            if (row == -1) {
                return;
            }

            DubboFavorite dubboFavorite = favoritesListModel.getElementAt(row);
            if(favoritesClickCallback != null){
                favoritesClickCallback.accept(dubboFavorite);
            }
        });

        historyListModel = new CollectionListModel<>();
        historyList = new DubboHistoryJList(historyListModel);
        historyList.addListSelectionListener(e -> {
            int row = historyList.getSelectedIndex();
            if (row == -1) {
                return;
            }

            DubboHistory dubboHistory = historyListModel.getElementAt(row);
            if(historyClickCallback != null){
                historyClickCallback.accept(dubboHistory);
            }
        });
    }

    @Override
    public void dispose() {

    }
}
